﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ColdSun.Commons;
using ColdSun.Commons.Configs;
using ColdSun.Services;
using ColdSun.Framework;

namespace ColdSun.BankManagements
{
    public partial class CtrlStatementCredit : UserControl
    {
        private FrmProgressBar frmProgressBar = new FrmProgressBar();
        private string result;

        public CtrlStatementCredit()
        {
            InitializeComponent();
        }

        private void CtrlSaoKe_Load(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(Utilities.GetFullFileName(CommonConfigs.DefaultOriginalFile)))
            {
                txtOriginalFile.Text = Utilities.GetFullFileName(CommonConfigs.DefaultOriginalFile);
            }
            if (System.IO.File.Exists(Utilities.GetFullFileName(CommonConfigs.DefaultFollowingFile)))
            {
                txtFollowingFile.Text = Utilities.GetFullFileName(CommonConfigs.DefaultFollowingFile);
            }
            if (System.IO.Directory.Exists(Utilities.GetFullFileName(CommonConfigs.DefaultExportFolder)))
            {
                txtExportFolder.Text = Utilities.GetFullFileName(CommonConfigs.DefaultExportFolder);
            }
            dtpReportDate.Format = DateTimePickerFormat.Custom;
            dtpReportDate.CustomFormat = "dd/MM/yyyy";
            txtDepartmentCodes.Text = CommonConfigs.DefaultExportDepartments;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            ExportToDoc();
        }

        private void ExportToDoc()
        {
            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundExportWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundExportWorker_RunWorkerCompleted);

            // Kick off the Async thread
            backgroundWorker.RunWorkerAsync();

            // Lock up the UI with this modal progress form.
            frmProgressBar.ShowDialog();
        }

        private void BackgroundExportWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Logger.Info(string.Format("----- BEGIN TO PROCESS STATEMENT CREDIT -----"));

            try
            {
                StatementCreditService excelService = new StatementCreditService();
                string reportDays = txtReportDays.Text;
                if (cbUsingReportDate.Checked)
                {
                    reportDays = dtpReportDate.Value.ToString("dd");
                }
                string[] reportDeparments = null;
                if (!cbReportAllDepartments.Checked)
                {
                    reportDeparments = txtDepartmentCodes.Text.Trim().Split(',');
                }

                this.result = excelService.GenerateStatementCredit(
                    txtOriginalFile.Text,
                    txtFollowingFile.Text,
                    dtpReportDate.Value,
                    reportDays,
                    reportDeparments,
                    txtExportFolder.Text);
            }
            catch (Exception ex)
            {
                Logger.Error("Background worker - Do work", ex);
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
            finally
            {
                Logger.Info(string.Format("----- STATEMENT CREDIT -----"));
            }
        }

        private void BackgroundExportWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (frmProgressBar != null)
            {
                frmProgressBar.Close();
            }
            if (!string.IsNullOrEmpty(this.result))
            {
                MessageBox.Show(this.result, "Result");
            }
        }

        private void cbUsingReportDate_CheckedChanged(object sender, EventArgs e)
        {
            if (cbUsingReportDate.Checked)
            {
                txtReportDays.Visible = false;
            }
            else
            {
                txtReportDays.Visible = true;
            }
        }

        private void btnOriginalFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Excel Files (.xlsx)|*.xlsx|Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtOriginalFile.Text = openFileDialog.FileName;
            }
        }

        private void btnFollowingFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Excel Files (.xlsx)|*.xlsx|Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFollowingFile.Text = openFileDialog.FileName;
            }
        }

        private void btnExportFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtExportFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void cbReportAllDepartments_CheckedChanged(object sender, EventArgs e)
        {
            if (cbReportAllDepartments.Checked)
            {
                txtDepartmentCodes.Visible = false;
            }
            else
            {
                txtDepartmentCodes.Visible = true;
            }
        }
    }
}
